package com.example.wm.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.wm.common.Result;
import com.example.wm.common.ResultData;
import com.example.wm.entity.Cartinandout;
import com.example.wm.mapper.CartinandoutMapper;
import com.example.wm.service.CartinandoutService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/Cartinandout")
//公车出入
public class CartinandoutController {
    @Resource
    private CartinandoutService cartinandoutService;
    @Resource
    private CartinandoutMapper cartinandoutMapper;
    @Transactional
    @PostMapping("/saveout")
    public Result saveoutAddress(@RequestBody Cartinandout cartinandout) {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        Date date = new Date(System.currentTimeMillis());
        String time = formatter.format(date);
        cartinandout.setOut_submit_time(time);
        Boolean b=  cartinandoutService.save(cartinandout);
        return ResultData.success(b);
    }

    @Transactional
    @PostMapping("/find")
    public Result findAddress(@RequestBody Cartinandout cartinandout) {
        LambdaQueryWrapper<Cartinandout> lq=new LambdaQueryWrapper<>();
//        lq.orderByDesc(Cartinandout::getOut_submit_time);
        lq.eq(Cartinandout::getType,"0");
        lq.eq(Cartinandout::getName,cartinandout.getName());

        List<Cartinandout> list= cartinandoutMapper.selectList(lq);
        return ResultData.success(list);
    }
    @Transactional
    @PostMapping("/savein")
    public Result saveinAddress(@RequestBody Cartinandout cartinandout) {
        SimpleDateFormat formatter= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss ");
        Date date = new Date(System.currentTimeMillis());
        String time = formatter.format(date);
        cartinandout.setBack_submit_time(time);
        Boolean b=  cartinandoutService.updateById(cartinandout);
        return ResultData.success(b);
    }
}
